package pl.edu.pk.mech.kkbus.controller;

import java.util.List;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pl.edu.pk.mech.kkbus.dto.Bus;
import pl.edu.pk.mech.kkbus.dto.Defect;
import pl.edu.pk.mech.kkbus.dto.Refueling;
import pl.edu.pk.mech.kkbus.model.ResponseStatus;
import pl.edu.pk.mech.kkbus.service.BusService;
import pl.edu.pk.mech.kkbus.service.DefectService;
import pl.edu.pk.mech.kkbus.service.RefuelingService;

@Controller
public class BusController extends RestController {
    
    private static final Logger log = Logger.getLogger(UserController.class.getName());
    
    @Autowired BusService busService;
    @Autowired DefectService defectService;
    @Autowired RefuelingService refuelingService; 
    
    @RequestMapping(value = "/busses", method = RequestMethod.GET)
    public @ResponseBody List<Bus> getBusses() {
        log.info("Get all busses");
        return busService.getAllBusses();       
    } 
    
    @RequestMapping(value = "/bus", method = RequestMethod.POST)
    public @ResponseBody ResponseStatus create(@RequestBody Bus bus) {
        boolean result = busService.create(bus);
        return result ? ResponseStatus.ADDED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/bus/{id}", method = RequestMethod.PUT)
    public @ResponseBody ResponseStatus update(@PathVariable long id, @RequestBody Bus bus) {
        boolean result = busService.update(id, bus);
        return result ? ResponseStatus.UPDATED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/bus/{id}", method = RequestMethod.DELETE)
    public @ResponseBody ResponseStatus delete(@PathVariable long id) {
        boolean result = busService.delete(id);
        return result ? ResponseStatus.DELETED : ResponseStatus.ERROR;
    }
    
    // Defect    
    @RequestMapping(value = "/bus/{id}/defect", method = RequestMethod.GET)
    public @ResponseBody List<Defect> getDefectForBus(@PathVariable long id) {
        log.info("Get defect for bus " + id);
        return busService.getDefectForBus(id);
    }
    
    @RequestMapping(value = "/defect", method = RequestMethod.POST)
    public @ResponseBody ResponseStatus createDefect(@RequestBody Defect defect) {
        log.info("Create defect for bus " + defect.getBusId());
    	boolean result = defectService.create(defect);
        return result ? ResponseStatus.ADDED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/defect/{id}", method = RequestMethod.PUT)
    public @ResponseBody ResponseStatus updateDefect(@PathVariable long id, @RequestBody Defect defect) {
        log.info("Update defect " + id);
    	boolean result = defectService.update(id, defect);
        return result ? ResponseStatus.UPDATED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/defect/{id}", method = RequestMethod.DELETE)
    public @ResponseBody ResponseStatus deleteDefect(@PathVariable long id) {
        log.info("Delete defect " + id);
    	boolean result = defectService.delete(id);
        return result ? ResponseStatus.DELETED : ResponseStatus.ERROR;
    }
    
    // Refueling    
    @RequestMapping(value = "/bus/{id}/refueling", method = RequestMethod.GET)
    public @ResponseBody List<Refueling> getRefuelingForBus(@PathVariable long id) {
        log.info("Get refueling for bus " + id);
        return busService.getRefuelingForBus(id);
    }
    
    @RequestMapping(value = "/refueling", method = RequestMethod.POST)
    public @ResponseBody ResponseStatus createRefueling(@RequestBody Refueling refueling) {
        log.info("Create refueling for bus " + refueling.getBusId());
    	boolean result = refuelingService.create(refueling);
        return result ? ResponseStatus.ADDED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/refueling/{id}", method = RequestMethod.PUT)
    public @ResponseBody ResponseStatus updateRefueling(@PathVariable long id, @RequestBody Refueling refueling) {
        log.info("Update refueling " + id);
    	boolean result = refuelingService.update(id, refueling);
        return result ? ResponseStatus.UPDATED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/refueling/{id}", method = RequestMethod.DELETE)
    public @ResponseBody ResponseStatus deleteRefueling(@PathVariable long id) {
        log.info("Delete refueling " + id);
    	boolean result = refuelingService.delete(id);
        return result ? ResponseStatus.DELETED : ResponseStatus.ERROR;
    }
}
